package com.pccc.batch;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class SpringBatchApplication {

	
	
	public static void main(String[] args) {
		
		boolean oneJob = false;
        boolean twoJob = false;
		
		SpringApplication application = new SpringApplication(SpringBatchApplication.class);
        application.setWebApplicationType(WebApplicationType.SERVLET);
        ConfigurableApplicationContext ctx = application.run(args);
        JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
 
        Job importJob = (Job) ctx.getBean("partitionJob");
        String path = "person1.csv";
        try {
        	jobLauncher.run(importJob, new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //无参数时，都执行
//        if (args.length == 0) {
//            oneJob = true;
//            twoJob = true;
//        } else {
//            for (String arg : args) {
//                if (arg.toLowerCase().equals("?")) {
//                    System.out.println("Usage : Batch [-b] [-r]");
//                }
//                if (arg.toLowerCase().equals("-o") || arg == null) {
//                    oneJob = true;
//                }
//                if (arg.toLowerCase().equals("-t")) {
//                    twoJob = true;
//                }
//            }
//        }
//        if (oneJob) {
//            JobParameters jobParameters = new JobParametersBuilder()
//                    .addDate("date", new Date())
//                    .toJobParameters();
//            try {
//				jobLauncher.run(ctx.getBean("jobOne", Job.class), jobParameters);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//           //jobOne名称必须和JobOneConfiguration中配置的@bean Job 的方法名一致，后面jobTwo也是一样。
//        }
//        if (twoJob) {
//            JobParameters jobParameters = new JobParametersBuilder()
//                    .addDate("date", new Date())          
//                    .toJobParameters();
//            try {
//				jobLauncher.run(ctx.getBean("jobTwo", Job.class), jobParameters);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//        }
 
        System.exit(0);

	}
}
